package com.gakj.truck.third.api.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.gakj.truck.third.api.service.UserDtoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 蜗牛用户相关操作
 * Created by gc on 2016/5/10.
 */

@RestController
@RequestMapping(value = "/api/user")
public class UserDtoController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserDtoController.class);

    @Autowired
    UserDtoService userDtoService;

    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public JSONObject checkUser(
            @RequestParam(value = "loginName", defaultValue = "") String loginName,
            @RequestParam(value = "password", defaultValue = "") String password,
            @RequestParam(value = "tel", defaultValue = "") String mobile,
            @RequestParam(value = "type", defaultValue = "user") String type
    ) {
        JSONObject result = userDtoService.checkUser(loginName, password, mobile, type);
        return result;
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public JSONObject register(
            @RequestParam(value = "loginName", defaultValue = "") String loginName,
            @RequestParam(value = "mobile", defaultValue = "") String mobile,
            @RequestParam(value = "password", defaultValue = "") String password,
            @RequestParam(value = "type", defaultValue = "user") String type
    ) {
        JSONObject result = new JSONObject();

        if(StringUtils.isEmpty(mobile)){
            result.put("resMsg", "手机号不能为空!");
            result.put("resCode", "0");
            return result;
        }
        if(StringUtils.isEmpty(password)){
            result.put("resMsg", "密码不能为空");
            result.put("resCode", "0");
            return result;
        }
        boolean flag = userDtoService.checkMobile(mobile);
        if (flag) {
            result.put("resMsg", "该手机号已经注册");
            result.put("resCode", "0");
            return result;
        }
        JSONObject json = userDtoService.registerUser(loginName, mobile, password, type);
        return json;
    }

}
